package com.hutao.manage.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.management.RuntimeErrorException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hutao.manage.datasource.TargetDataSource;
import com.hutao.manage.mapper.CitizensInfoMapper;
import com.hutao.manage.mapper.DicMapper;
import com.hutao.manage.mapper.FirstLevelMedicalMapper;
import com.hutao.manage.mapper.HospitalizationNoteMapper;
import com.hutao.manage.mapper.MedicalResourceMapper;
import com.hutao.manage.mapper.MedicalResourcePicMapper;
import com.hutao.manage.mapper.ScanFileMapper;
import com.hutao.manage.mapper.TreatmentTraceMapper;
import com.hutao.manage.mapper.UploadScanMapper;
import com.hutao.manage.pojo.CitizensInfo;
import com.hutao.manage.pojo.FirstLevelMedical;
import com.hutao.manage.pojo.HospitalizationNote;
import com.hutao.manage.pojo.MedicalResource;
import com.hutao.manage.pojo.MedicalResourcePic;
import com.hutao.manage.pojo.TreatmentTrace;
import com.hutao.manage.pojo.UploadScan;
import com.hutao.manage.query.MedBindQuery;
import com.hutao.manage.util.Params;
import com.hutao.manage.util.StringUtil;

@Service
public class MedBindService {

	@Autowired
	private UploadScanMapper uploadScanMapper;
	
	@Autowired
	private ScanFileMapper scanFileMapper;
	
	@Autowired
	private DicMapper dicMapper;
	
	@Autowired
	private CitizensInfoMapper citizensInfoMapper;
	
	@Autowired
	private FirstLevelMedicalMapper firstLevelMedicalMapper;
	
	@Autowired
	private HospitalizationNoteMapper hospitalizationNoteMapper;
	
	@Autowired
	private MedicalResourceMapper medicalResourceMapper;
	
	@Autowired
	private MedicalResourcePicMapper medicalResourcePicMapper;
	
	@Autowired
	private TreatmentTraceMapper treatmentTraceMapper;
	
/*	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	public List<Map<String,Object>> getUnBindList(Params params){
		return uploadScanMapper.getUnBindList(params);
	}
	*/
/*	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	public List<Map<String,Object>> getBindList1(Params params){
		return uploadScanMapper.getBindList(params);
	}*/
	
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	public List<Map<String,Object>> getBindList(MedBindQuery params){
		return uploadScanMapper.getBindList(params);
	}
	
/*	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	public int getUnBindCount(Params params){
		return uploadScanMapper.getUnBindCount(params);
	}*/
	
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	public int getBindCount(MedBindQuery params){
		return uploadScanMapper.getBindCount(params);
	}
	
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	public List<Map<String,Object>> fileUrlList(Integer uploadScanId){
		List<Map<String,Object>> fileMsgMap=scanFileMapper.getFileMsgList(uploadScanId);
		for (Map<String, Object> map : fileMsgMap) {
			System.out.println(map);
			//{fileName=qxojfum4msqwnx0xywklt89o968viqt53o6jc430p3hmi0jwrl2thdyme8l9jcm5lxvmaohbr5hj48q5bay6snnkcc6e005aj.png, uploadScanId=113, fileHash=07a3jigiqxojfum4msqwnx0xywklt89o968viqt53o6jc430p3hmi0jwrl2thdyme8l9jcm5lxvmaohbr5hj48q5bay6snnkcc6e, fileUrl=07/a3/ji/gi/}
		}
		return fileMsgMap;
	}
	
	/**
	 * 根据表名称查询字典信息
	 * @param tableName
	 * 		表名称
	 * @return
	 */
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	public List<Map<String,Object>> getSexDicMsgList(){
		return dicMapper.getDicList();
	}
	
	/**
	 * 根据表名称查询字典信息
	 * @param tableName
	 * 		表名称
	 * @return
	 */
	@TargetDataSource(name="ds1")
	@Transactional(propagation=Propagation.REQUIRED)
	public List<Map<String,Object>> getMarriageList(){
		return dicMapper.getMarriageList();
	}
	
	/**
	 *查询医院字典信息
	 * @return
	 */
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	public List<Map<String,Object>> getHospitalList(){
		return dicMapper.getHospitalList();
	}
	
	/**
	 * 查询就诊类型信息
	 */
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	public List<Map<String,Object>> getTreatmentTypeList(){
		return dicMapper.getTreatmentTypeList();
	}
	
	/**
	 * 查询就诊级别信息
	 */
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	public List<Map<String,Object>> getTreatmentSignList(){
		return dicMapper.getTreatmentSignList();
	}
	
	/**
	 *查询入院方式信息
	 * @return
	 */
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	public List<Map<String,Object>> getInHospitalTypeList(int hospitalId){
		return dicMapper.getInHospitalTypeList(hospitalId);
	}
	
	/**
	 *查询出院方式信息
	 * @return
	 */
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	public List<Map<String,Object>> getOutHospitalTypeList(int hospitalId){
		return dicMapper.getOutHospitalTypeList(hospitalId);
	}
	
	/**
	 *查询医疗科室信息
	 * @return
	 */
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	public List<Map<String,Object>> getMedicalOfficeList(int hospitalId){
		return dicMapper.getMedicalOfficeList(hospitalId);
	}
	
	@TargetDataSource(name="ds1")
	@Transactional(propagation=Propagation.REQUIRED)
	public boolean addPatient(CitizensInfo citizensInfo,FirstLevelMedical firstLevelMedical,HospitalizationNote hospitalizationNote,Integer uploadScanId,String currentUser){
		try {
			String guid=StringUtil.getId();
			citizensInfo.setGuid(guid);
			citizensInfo.setCreateDate(new Date());
			citizensInfo.setStatus(1);
			citizensInfoMapper.insert(citizensInfo);
			
			firstLevelMedical.setGuid(StringUtil.getId());
			firstLevelMedical.setCreateDate(new Date());
			firstLevelMedical.setCitizensGuid(guid);
			firstLevelMedical.setStatus(1);
			firstLevelMedicalMapper.insert(firstLevelMedical);
			
			TreatmentTrace treatmentTrace = new TreatmentTrace();
			treatmentTrace.setUserId(currentUser);
			List<Map<String,Object>> treatmentTyps = treatmentTraceMapper.getTreatmentTraceTypeByCode(TreatmentTrace.VISIT_INIT);
			treatmentTrace.setTraceTypeId((Integer)treatmentTyps.get(0).get("id"));
			treatmentTrace.setTreatmentGuid(firstLevelMedical.getGuid());
			treatmentTraceMapper.insert(treatmentTrace);
			hospitalizationNote.setFirstLevelMedicalGuid(firstLevelMedical.getGuid());
			hospitalizationNoteMapper.insert(hospitalizationNote);
			UploadScan uploadScan = uploadScanMapper.selectByPrimaryKey(uploadScanId);
			uploadScan.setFirstLevelMedicalGuid(firstLevelMedical.getGuid());
			uploadScanMapper.updateByPrimaryKey(uploadScan);
			
			List<String> fileHashs=uploadScanMapper.getFileHashByUploadScanId(uploadScanId);
			if(fileHashs==null || fileHashs.size()==0){
				return false;
			}
			MedicalResource medicalResource=null;
			MedicalResourcePic medicalResourcePic=null;
			for (String fileHash : fileHashs) {
				medicalResource=new MedicalResource();
				medicalResource.setFirstLevelMedicalId(firstLevelMedical.getGuid());
				medicalResource.setMedicalResourceTypeId(1);
				medicalResource.setCreateDate(new Date());
				medicalResourceMapper.insert(medicalResource);
				
				medicalResourcePic=new MedicalResourcePic();
				medicalResourcePic.setFileHash(fileHash);
				medicalResourcePic.setMedicalResourceId(medicalResource.getId());
				medicalResourcePicMapper.insert(medicalResourcePic);
			}
			treatmentTrace = new TreatmentTrace();
			treatmentTrace.setUserId(currentUser);
			treatmentTyps = treatmentTraceMapper.getTreatmentTraceTypeByCode(TreatmentTrace.VISIT_PIGEONHOLE);
			treatmentTrace.setTraceTypeId((Integer)treatmentTyps.get(0).get("id"));
			treatmentTrace.setTreatmentGuid(firstLevelMedical.getGuid());
			treatmentTraceMapper.insert(treatmentTrace);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		
			return false; 
		}
	}
	
	
	/**
	 * 根据姓名和身份证信息获取患者信息
	 * @param params
	 * @return
	 */
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	public List<Map<String,Object>> getPatientMsg(Params params){
		
		List<Map<String,Object>> citizenMsgList=firstLevelMedicalMapper.getCitizenByNameAndIdNumber(params);
		if(citizenMsgList!=null && citizenMsgList.size()>0){
			for (Map<String, Object> map : citizenMsgList) {
				System.out.println(map.get("guid"));
				String citizensGuid=(String) map.get("guid");
				List<Map<String,Object>> patientMsgs=firstLevelMedicalMapper.getPatientMsg(citizensGuid);
				map.put("patientMsgList", patientMsgs);
			}
		}
		return citizenMsgList;
	}
	
	
	@TargetDataSource(name="ds1")
	@Transactional(propagation=Propagation.REQUIRED)
	public boolean bindUrlByFileHashAndUploadScan(String firstLeveMedGuid,Integer uploadScanId,String currentUser){
		List<String> fileHashs=uploadScanMapper.getFileHashByUploadScanId(uploadScanId);
		if(fileHashs==null || fileHashs.size()==0){
			return false;
		}
		MedicalResource medicalResource=null;
		MedicalResourcePic medicalResourcePic=null;
		for (String fileHash : fileHashs) {
			medicalResource=new MedicalResource();
			medicalResource.setFirstLevelMedicalId(firstLeveMedGuid);
			medicalResource.setMedicalResourceTypeId(1);
			medicalResource.setCreateDate(new Date());
			medicalResourceMapper.insert(medicalResource);
			
			medicalResourcePic=new MedicalResourcePic();
			medicalResourcePic.setFileHash(fileHash);
			medicalResourcePic.setMedicalResourceId(medicalResource.getId());
			medicalResourcePicMapper.insert(medicalResourcePic);
		}
		UploadScan uploadScan=new UploadScan();
		uploadScan.setId(uploadScanId);
		uploadScan.setFirstLevelMedicalGuid(firstLeveMedGuid);
		//uploadScan.setStatus(2);
		uploadScanMapper.updateByPrimaryKeySelective(uploadScan);
		TreatmentTrace treatmentTrace = new TreatmentTrace();
		treatmentTrace.setUserId(currentUser);
		List<Map<String,Object>> treatmentTyps = treatmentTraceMapper.getTreatmentTraceTypeByCode(TreatmentTrace.VISIT_PIGEONHOLE);
		treatmentTrace.setTraceTypeId((Integer)treatmentTyps.get(0).get("id"));
		treatmentTrace.setTraceTypeId(2);
		treatmentTrace.setTreatmentGuid(firstLeveMedGuid);
		treatmentTraceMapper.insert(treatmentTrace);
		return true;
	}
	
	
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	public boolean isHaveMedResource(String firstLeveMedGuid){
		List<Map<String,Object>> medicalResources=medicalResourceMapper.getMedResourceByFirstLevelGuid(firstLeveMedGuid);
		if(medicalResources!=null  && medicalResources.size()>0){
			return true;
		}
		return false;
	}
	
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	public CitizensInfo getCitizenByGuid(String guid){
		return citizensInfoMapper.selectByPrimaryKey(guid);
	}
	
	
	@TargetDataSource(name="ds1")
	@Transactional(propagation=Propagation.REQUIRED)
	public boolean addMedical(FirstLevelMedical firstLevelMedical,HospitalizationNote hospitalizationNote,String citizenGuid,Integer uploadScanId,String currentUser){
		try {
			
			firstLevelMedical.setGuid(StringUtil.getId());
			firstLevelMedical.setCreateDate(new Date());
			firstLevelMedical.setCitizensGuid(citizenGuid);
			firstLevelMedical.setStatus(1);
			firstLevelMedicalMapper.insert(firstLevelMedical);
			
			TreatmentTrace treatmentTrace = new TreatmentTrace();
			treatmentTrace.setUserId(currentUser);
			List<Map<String,Object>> treatmentTyps = treatmentTraceMapper.getTreatmentTraceTypeByCode(TreatmentTrace.VISIT_INIT);
			treatmentTrace.setTraceTypeId((Integer)treatmentTyps.get(0).get("id"));
			treatmentTrace.setTreatmentGuid(firstLevelMedical.getGuid());
			treatmentTraceMapper.insert(treatmentTrace);
			
			hospitalizationNote.setFirstLevelMedicalGuid(firstLevelMedical.getGuid());
			hospitalizationNoteMapper.insert(hospitalizationNote);
			
			UploadScan uploadScan = uploadScanMapper.selectByPrimaryKey(uploadScanId);
			uploadScan.setFirstLevelMedicalGuid(firstLevelMedical.getGuid());
			uploadScanMapper.updateByPrimaryKey(uploadScan);
			
			List<String> fileHashs=uploadScanMapper.getFileHashByUploadScanId(uploadScanId);
			if(fileHashs==null || fileHashs.size()==0){
				return false;
			}
			MedicalResource medicalResource=null;
			MedicalResourcePic medicalResourcePic=null;
			for (String fileHash : fileHashs) {
				medicalResource=new MedicalResource();
				medicalResource.setFirstLevelMedicalId(firstLevelMedical.getGuid());
				medicalResource.setMedicalResourceTypeId(1);
				medicalResource.setCreateDate(new Date());
				medicalResourceMapper.insert(medicalResource);
				
				medicalResourcePic=new MedicalResourcePic();
				medicalResourcePic.setFileHash(fileHash);
				medicalResourcePic.setMedicalResourceId(medicalResource.getId());
				medicalResourcePicMapper.insert(medicalResourcePic);
			}
			treatmentTrace = new TreatmentTrace();
			treatmentTrace.setUserId(currentUser);
			treatmentTyps = treatmentTraceMapper.getTreatmentTraceTypeByCode(TreatmentTrace.VISIT_PIGEONHOLE);
			treatmentTrace.setTraceTypeId((Integer)treatmentTyps.get(0).get("id"));
			treatmentTrace.setTreatmentGuid(firstLevelMedical.getGuid());
			treatmentTraceMapper.insert(treatmentTrace);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
		return true;
	}
	
	
	@TargetDataSource(name="ds1")
	@Transactional(propagation=Propagation.REQUIRED)
	public boolean bindMedicalRecordImage(String mrId,Integer visitNumber,String currentUser){
		try {
			UploadScan uploadScan = uploadScanMapper.getUploadScanByFolderName(mrId+"-"+visitNumber);
			List<String> visitGuids = firstLevelMedicalMapper.getFirstGuidByMrIdAndVisitNumber(mrId,visitNumber);
			if(visitGuids.isEmpty() || uploadScan == null){
				return false;
			}
			String visitGuid = visitGuids.get(0);
			uploadScan.setFirstLevelMedicalGuid(visitGuid);
			uploadScanMapper.updateByPrimaryKey(uploadScan);
			List<String> fileHashs=uploadScanMapper.getFileHashByUploadScanId(uploadScan.getId());
			if(fileHashs==null || fileHashs.size()==0){
				return false;
			}
			MedicalResource medicalResource=null;
			MedicalResourcePic medicalResourcePic=null;
			for (String fileHash : fileHashs) {
				medicalResource=new MedicalResource();
				medicalResource.setFirstLevelMedicalId(visitGuid);
				medicalResource.setMedicalResourceTypeId(1);
				medicalResource.setCreateDate(new Date());
				medicalResourceMapper.insert(medicalResource);
				
				medicalResourcePic=new MedicalResourcePic();
				medicalResourcePic.setFileHash(fileHash);
				medicalResourcePic.setMedicalResourceId(medicalResource.getId());
				medicalResourcePicMapper.insert(medicalResourcePic);
			}
			TreatmentTrace treatmentTrace = new TreatmentTrace();
			treatmentTrace.setUserId(currentUser);
			List<Map<String,Object>> treatmentTyps = treatmentTraceMapper.getTreatmentTraceTypeByCode(TreatmentTrace.VISIT_PIGEONHOLE);
			treatmentTrace.setTraceTypeId((Integer)treatmentTyps.get(0).get("id"));
			treatmentTrace.setTreatmentGuid(visitGuid);
			treatmentTraceMapper.insert(treatmentTrace);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
		
		return true;
	}
}
